package com.jp.knowledge.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jp.knowledge.R;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.TeamCircleModel;
import com.jp.knowledge.model.UserData;

import java.util.ArrayList;

/**
 * Created by 528787067@qq.com on 2017/7/20.
 */

public class TeamCircleDataShowDetailFragment extends TeamCircleDataShowFragment {

	private static final int GET_DETAIL_DATA_CODE = 0x06;

	public static TeamCircleDataShowDetailFragment newInstance(UserData userData, String id){
		TeamCircleDataShowDetailFragment fragment = new TeamCircleDataShowDetailFragment();
		Bundle bundle = new Bundle();
		bundle.putString("id", id);
		bundle.putSerializable("userData", userData);
		fragment.setArguments(bundle);
		return fragment;
	}

	private String id;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		if(context != null)
			return;
		super.onCreate(savedInstanceState);
		id = getArguments().getString("id");
	}

	@Override
	protected void initView() {
		super.initView();
		canRefreshLayout.setLoadMoreEnabled(false);
		canRefreshLayout.setRefreshEnabled(false);
		teamCircleAdapter.addItemClickListener(null);
		contentView.findViewById(R.id.team_circle_empty_bg).setVisibility(View.GONE);
	}

	@Override
	protected void updateRemindView() {
		teamCircleHeader.setVisibility(View.GONE);
	}

	@Override
	protected void getData(int type, int mode) {
		JsonObject param = new JsonObject();
		param.addProperty("scoopId", id);
		HttpHelper.getInstance(context).scoopDetailsData(param, GET_DETAIL_DATA_CODE, this);
	}

	@Override
	public void onNext(IModel data, int requestType) {
		if(data.getErrcode() == 0 && requestType == GET_DETAIL_DATA_CODE){
			TeamCircleModel teamCircleModel = data.getEntity(TeamCircleModel.class);
			if(teamCircleModel == null)
				return;
			if(teamCircleModel.getId() == null || teamCircleModel.getId().trim().length() == 0){
				contentView.findViewById(R.id.team_circle_empty_bg).setVisibility(View.VISIBLE);
				((ImageView)contentView.findViewById(R.id.team_circle_empty_img)).setImageResource(R.mipmap.no_data_hint);
				((TextView)contentView.findViewById(R.id.team_circle_empty_text)).setText("内容已被删除！");
				canRefreshLayout.setVisibility(View.GONE);
				return;
			}
			if(teamCircleModels == null)
				teamCircleModels = new ArrayList<>();
			else
				teamCircleModels.clear();
			teamCircleModels.add(teamCircleModel);
			teamCircleAdapter.updateAdapter(teamCircleModels);
		} else {
			super.onNext(data, requestType);
		}
		if(requestType == DELETE && teamCircleAdapter.getItemCount() == 0){
			getActivity().onBackPressed();
			localBroadcastManager.sendBroadcast(new Intent(LocalBroadCast.TEAM_CIRCLE_DATA_DELETE)
					.putExtra("scoopId", id));
		}
	}

	@Override
	protected void readDataFromCache() {
		getData(0, 0);
	}

	@Override
	protected void saveDataToCache() {
		if(teamCircleModels == null || teamCircleModels.size() == 0)
			return;
		localBroadcastManager.sendBroadcast(new Intent(LocalBroadCast.TEAM_CIRCLE_DATA_CHANGE)
				.putExtra("data", teamCircleModels.get(0)));
	}
}
